rd_common.js ➔ fecha   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
c 0
b 0
f 0
dl 0
loc 22
rs 9.5
1
/* 
2
 * Copyright (C) 2016 Joe Nilson <joenilson at gmail.com>
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 * Funcion para generar el daterangepicker en modo rango de fechas
20
 * @param {string} f_rango es el componente donde se mostrará el calendario
21
 * @param {string} f_desde es el id del input donde grabaremos la fecha de inicio
22
 * @param {string} f_hasta es el id del input donde grabaremos la fecha de fin
23
 * @param {string} formato es el formato en que guardaremos y mostraremos la fecha
24
 * @param {boolean} rangos es un campo para saber si mostramos o no el selector de rangos predefinidos
25
 * @param {boolean} tiempos es un campo booleano para saber si mostramos el selector de tiempo hora, minuto
26
 * @returns {empty} devuelve el selector con el rango de fechas
27
 */
28
function rango_fechas(f_rango, f_desde, f_hasta, formato, rangos, tiempos){
29
    moment().format(formato);
30
    if(typeof($('#'+f_rango)) !== 'undefined'){
31
        $('#'+f_rango).daterangepicker({
32
            singleDatePicker: false,
33
            showDropdowns: true,
34
            timePicker: tiempos,
35
            timePickerIncrement: (tiempos)?5:0,
36
            ranges: (rangos)?{
37
                'Hoy': [moment(), moment()],
38
                'Ayer': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
39
                'Ultimos 7 Días': [moment().subtract(6, 'days'), moment()],
40
                'Ultimos 30 días': [moment().subtract(29, 'days'), moment()],
41
                'Este mes': [moment().startOf('month'), moment().endOf('month')],
42
                'Anterior Mes': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
43
            }:null,
44
            locale: {
45
                format: formato,
46
                separator: " - ",
47
                applyLabel: "Tomar",
48
                cancelLabel: "Cancelar",
49
                fromLabel: "Desde",
50
                toLabel: "Hasta",
51
                customRangeLabel: "Manual"
52
            },
53
            opens: "right",
54
            startDate: moment().startOf('month'),
55
            endDate: moment()
56
        });
57
        
58
        $('#'+f_rango).on('apply.daterangepicker', function(ev, picker) {
59
            $('#'+f_desde).val(picker.startDate.format(formato));
60
            $('#'+f_hasta).val(picker.endDate.format(formato));
61
        });
62
        
63
        if($('#'+f_desde).val()){
64
            $('#'+f_rango).data('daterangepicker').setStartDate($('#'+f_desde).val());
65
        }else{
66
            $('#'+f_desde).val($('#'+f_rango).data('daterangepicker').startDate.format(formato));
67
        }
68
69
        if($('#'+f_hasta).val()){
70
            $('#'+f_rango).data('daterangepicker').setEndDate($('#'+f_hasta).val());
71
        }else{
72
            $('#'+f_hasta).val($('#'+f_rango).data('daterangepicker').endDate.format(formato));
73
        }
74
    }
75
}
76
77
/*
78
 * Funcion para generar el daterangepicker en modo single
79
 * @param id_field es el id donde llamaremos al calendario
80
 * @param formato es el formato en que necesitamos la fecha
81
 * @param tiempos es un cambo boolean si necesitamos o no la parte de tiempo hora, minuto
82
 */
83
function fecha(id_field, formato, tiempos){
84
    moment().format(formato);
85
    if(typeof($('#'+id_field)) !== 'undefined'){
86
        $('#'+id_field).daterangepicker({
87
            singleDatePicker: true,
88
            showDropdowns: true,
89
            timePicker: tiempos,
90
            timePickerIncrement: (tiempos)?5:0,
91
            locale: {
92
                format: formato,
93
                separator: " - ",
94
                applyLabel: "Tomar",
95
                cancelLabel: "Cancelar",
96
                fromLabel: "Desde",
97
                toLabel: "Hasta",
98
                customRangeLabel: "Manual"
99
            },
100
            opens: "left",
101
            startDate: moment()
102
        });
103
    }
104
}
105